﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DB = HTComics.Data;
using System.Web.UI.HtmlControls;

namespace HTComics.Web
{
    public partial class ViewComics : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)        
        {
            LoadData();
        }

        private void LoadData()
        {
            string searchText = Request.QueryString["q"];
            SearchType sType = (SearchType)Convert.ToInt32(Request.QueryString["t"]);
            string header = "";
            HtmlGenericControl ul = new HtmlGenericControl("ul");
            
            switch (sType)
            {
                case SearchType.ByAlphabet:
                    if (Request.QueryString["q"] == "NUM")
                    {
                        header = "Found XX comic title(s) starting with numbers [0-9]";
                        searchText = "#";
                    }
                    else
                    {
                        header = "Found XX comic title(s) starting with letter " + Request.QueryString["q"];
                        searchText += "%";
                    }
                    break;

                case SearchType.ByText:
                    header = "Found XX comic title(s) that contains " + Request.QueryString["q"];
                    searchText = "%" + searchText + "%";
                    break;
            }

            DataTable dtResults = DB.Search.SearchComics(searchText.Replace("'","''"));

            searchHeader.InnerHtml = header.Replace("XX",dtResults.Rows.Count.ToString());

            if (dtResults.Rows.Count == 0)
            {
                searchContent.InnerHtml = "No data found";
            }
            else
            {
                for (int i = 0; i <= dtResults.Rows.Count - 1; i++)
                {
                    
                    if (dtResults.Rows[i]["NumIssues"] == DBNull.Value || Convert.ToInt32(dtResults.Rows[i]["NumIssues"]) == 0)
                        ul.InnerHtml += "<li class=\"ui-stat-default\" style=\"padding:5px;\"><DEL>" + dtResults.Rows[i]["Title"] + "</DEL> <span class=\"outOfStock\">(Out of Stock)</span></li>";
                    else
                        ul.InnerHtml += "<li class=\"ui-stat-default\" style=\"padding:5px;\"><a href=\"ViewIssues.aspx?cid=" + dtResults.Rows[i]["ComicId"] + "&ct=" + dtResults.Rows[i]["Title"] + "\">" + dtResults.Rows[i]["Title"] + " (" + dtResults.Rows[i]["NumIssues"] + " issues)</a></li>";
                     
                }

                searchContent.Controls.Add(ul);
            }


            //<ul>
                //<li class="ui-stat-default">Search 1</li>
            //</ul>
        }
    }
}